life <- read_csv(file = "lifeexpectancy.csv")
## Rows: 3306 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (4): Country Name, Country Code, Region, IncomeGroup
## dbl (12): Year, Life Expectancy World Bank, Prevelance of Undernourishment, ...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
treecover1 <- read_csv(file = "treecover1.csv")
## Rows: 4640 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): iso
## dbl (3): umd_tree_cover_loss__year, umd_tree_cover_loss__ha, gfw_gross_emiss...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
agriculture <- read_csv(file = "yield.csv")
## Rows: 56717 Columns: 12
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (6): Domain Code, Domain, Area, Element, Item, Unit
## dbl (6): Area Code, Element Code, Item Code, Year Code, Year, Value
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
iso <- read_csv(file = "iso.csv")
## Rows: 249 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): iso2, iso3, iso_num, country, country_common
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
fao1 <- read_csv(file = "fao1.csv")
## Rows: 65083 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (10): Domain Code, Domain, Area Code (M49), Area, Element, Item, Unit, F...
## dbl  (5): Element Code, Item Code, Year Code, Year, Value
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
fao2 <- read_csv(file = "fao2.csv")
## Rows: 19975 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (9): Domain Code, Domain, Area Code (M49), Area, Element, Item, Unit, Fl...
## dbl (5): Element Code, Item Code, Year Code, Year, Value
## lgl (1): Note
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
meat <- read_csv(file = "meat.csv")
## Rows: 14382 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Entity, Code
## dbl (2): Year, Meat, total | 00001765 || Production | 005510 || tonnes
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
urban <- read_csv(file = "urban.csv")
## Rows: 31571 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): Economy Label, Absolute value in thousands, Absolute value in thous...
## dbl (2): Year, Economy
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
gdp <- read_csv(file="gdp.csv")
## Rows: 196 Columns: 47
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (2): country_name, indicator_name
## dbl (45): 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, ...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
population <- read_csv(file = "population.csv")
## Rows: 12595 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Country Name
## dbl (2): Year, Count
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
library(dplyr)

life1 <- merge(population, life)

agriculture1 <- agriculture %>%
  select("Domain", "Area", "Item","Year", "Unit", "Value") %>%
  group_by(Year, Area) %>% 
  summarise(ExpansionSum = sum(Value), .groups = 'keep')

agriculture2 <- agriculture1 %>%
  rename("country" = Area)
merging <- merge(agriculture2, iso)

together <- merging %>%
  select("country", "Year", "ExpansionSum","iso3") %>%
  rename("iso" = iso3)

treecover <- treecover1 %>%
  rename("Year" = umd_tree_cover_loss__year)

merging3 <- merge(together, treecover)

merging3 <- merging3 %>%
  rename("Country" = "country") 

merging4 <- merging3 %>%
  rename("Treeloss" = "umd_tree_cover_loss__ha")  %>%
  rename("Co2emissions" = "gfw_gross_emissions_co2e_all_gases__Mg")

TreeLoss <- merging4 %>%
  group_by(Year,Country) %>% 
  summarise(Treeloss = sum(Treeloss), .groups = 'keep')

Co2emissions <- merging4 %>%
  group_by(Year,Country) %>% 
  summarise(Co2emissions = sum(Co2emissions), .groups = 'keep')

meat1 <- meat %>%
  rename("Country" = "Entity") %>%
  rename("Meatvalue" = "Meat, total | 00001765 || Production | 005510 || tonnes") %>% 
  group_by(Year,Country) %>% 
  summarise(Meatsum = sum(Meatvalue), .groups = 'keep')

FAO <- rbind(fao1, fao2) %>%
  .[,c(4,8,10,11,12)]

FAO_grouped <- FAO %>%
  rename("Country" = "Area") %>%
  split(FAO$Unit)

FAO_Volume <- FAO_grouped[[1]] %>%
  .[,-4]

FAO_Ton <- FAO_grouped[[2]]  %>%
  .[,-4]

FAO_Volume_sum <- FAO_Volume %>% 
  group_by(Year,Country) %>% 
  summarise(Forest_Volumesum = sum(Value), .groups = 'keep')

FAO_Ton_sums <- FAO_Ton %>% 
  group_by(Year, Country) %>% 
  summarise(Forest_Tonsum = sum(Value), .groups = 'keep')

merging5 <- merge(merging4, meat1)

merging6 <- merge(merging5, TreeLoss)

merging7 <- merge(merging6, Co2emissions)

merge8 <- merge(merging7, FAO_Ton_sums)

merge9 <- merge(merge8, FAO_Volume_sum)

urban1 <- urban %>%
  rename("Country" = "Economy Label") %>%
  rename("UrbanPop" = "Absolute value in thousands") %>%
  rename("Urban%" = "Urban population as percentage of total population") %>%
  select(-"Economy", -"Absolute value in thousands Missing value", -"Urban population as percentage of total population Missing value") %>%
  mutate(
    UrbanPop = as.numeric(UrbanPop),
    `Urban%` = as.numeric(`Urban%`)
  )
## Warning: There were 2 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `UrbanPop = as.numeric(UrbanPop)`.
## Caused by warning:
## ! NAs introduced by coercion
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
life3 <- life1 %>%
  rename("Country" = "Country Name")

merge10 <- merge(life3, merge9) 

merge11 <- merge(merge10, urban1)

filtered_GDP = gdp %>%
  select(-starts_with("19"))

reshaped_GDP <- filtered_GDP %>%
  pivot_longer(cols = starts_with("20"),
               names_to = "year",      
               values_to = "value")

gdpfinal <- reshaped_GDP %>%
  rename("Country" = "country_name") %>%
  rename("Year" = "year") %>%
  rename("GDP growth %" = "value") %>%
  select(-"indicator_name")

lifeexp <- merge(merge11, gdpfinal) %>%
  select(-iso, -Count)  %>%
  rename("GDP" = "GDP growth %")  %>%
  rename("Expectancy" = "Life Expectancy World Bank")

# Keep only columns without NA values
columns_to_keep <- c("Country", "Year", "Country Code", "Region", "IncomeGroup", 
                     "Injuries", "Communicable", "NonCommunicable", 
                     "Co2emissions", "Treeloss", "ExpansionSum", "Meatsum", "Forest_Tonsum")

lifeexp <- lifeexp[, columns_to_keep]

write.csv(lifeexp, "full_life_data.csv", row.names = FALSE)

Introduction